/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/12 19:05
 * @Copyright
 */
package multithreading;

/*
2.利用多线程求解某范围素数,每个线程负责1000范围：线程1找1-1000；线程2找1001-2000；
线程3找2001-3000。编程程序将每个线程找到的素数及时打印。
 */
public class sushu {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Processor21(2, 1000);
        Thread t2 = new Processor21(1001, 2000);
        Thread t3 = new Processor21(2001, 3000);
        t1.setName("线程1");
        t2.setName("线程2");
        t3.setName("线程3");
        t1.start();
        Thread.sleep(200);
        t2.start();
        Thread.sleep(200);
        t3.start();
    }
}

class Processor21 extends Thread {
    private int start;
    private int end;

    public Processor21(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public void run() {
        for (int i = start; i < end; i++) {
            boolean flag = true;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    flag = false;
                }
            }
            if (flag) {
                System.out.println(Thread.currentThread().getName() + "素数有：" + i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}